package com.maserb.monitor.web.jw_our;


import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.JwReport.JwReportEntity;
import com.maserb.monitor.entity.JwReport.JwReportFileEntity;
import com.maserb.monitor.entity.jwOur.JwFilePhotoEntity;
import com.maserb.monitor.entity.jwOur.JwMessageEntity;
import com.maserb.monitor.entity.jwOur.JwOurEntity;
import com.maserb.monitor.entity.jwQuestion.JwQuestionEntity;
import com.maserb.monitor.entity.jwQuestion.JwQuestionUserEntity;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.jwOur.JwFilePhotoService;
import com.maserb.monitor.service.jwOur.JwMessageService;
import com.maserb.monitor.service.jwOur.JwOurService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

//留言板（云端话廉）
@Controller
@RequestMapping("/jwMessageApp")
public class JwMessageAppController {
    @Autowired
    private JwMessageService service;
    @Autowired
    private JwFilePhotoService photoService;

    //  提交留言板  uuid,phoneModel,description,filePhotoIds字段
    @RequestMapping("/saveMessage")
    @ResponseBody
    public AjaxResult saveMessage(HttpServletRequest request,JwMessageEntity entity){
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                String filePhotoIds = request.getParameter("filePhotoIds");
                entity.setCreateTime(CreateTimeUtils.getCreateTime());
                entity.setIsDel("0");
                //不用发布，直接显示，若需要发布，改为0
                entity.setIsRelease("1");
                service.save(entity);
                try{
                    saveReportFile(entity,filePhotoIds);
                }catch(Exception e){

                }
                return "success";
            }
        });
    }
    void saveReportFile(JwMessageEntity messageEntity, String filePhotoIds){
        String [] fileInfoArr = filePhotoIds.split(",");
        for(int i=0;i<fileInfoArr.length;i++){
            JwFilePhotoEntity entity = photoService.findOne(fileInfoArr[i]);
            entity.setParent(messageEntity);
            photoService.save(entity);
        }
    }


    //  获取历史留言记录
    @RequestMapping("/doMessageList")
    @ResponseBody
    public PageResponseResult<JwMessageEntity> doMessageList(PageRequestParam page) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        String isRelease = "1";//是否发布
        String description = "";
        return AjaxUtils.processPage(page, sort, new Func_T1<PageRequest, Page<JwMessageEntity>>() {
            @Override
            public Page<JwMessageEntity> invoke(PageRequest pageRequest) throws Exception {
                Page<JwMessageEntity> dicPage = service.findAll(pageRequest, isRelease,description);
                return dicPage;
            }
        });
    }

}
